
//返回数组中属性值  data: Object[] 数据,
//                 key:string 标记字段,
//                 children：string 子类字段,
let Cesium=window.Cesium;

export const filterKey = (data, key,children='children')=>{
    let checkedKey =[];
    data.map(item=>{
        checkedKey.push(item[key])
    })
    return checkedKey
}

//标记函数辅助      data: Object[] 数据,
//                 key:string 标记字段,
//                 children：string 子类字段,
//                 retuKey：string  返回字段

const signKeys= (data, key,children,retuKey)=>{
       let retuKeys = []
       for(let i = 0; i<data.length;i++){
            if(data[i][key]){
               retuKeys.push(data[i][retuKey]);
            }
            if(data[i].children){
                signKeys(data[i].children,key,children)
            }
       }
       return retuKeys;
}
// 返回标记数组     data: Object[] 数据,
//                 key:string 标记字段,
//                 children：string 子类字段,
//                 retuKey：string  返回字段
export const signKey = (data, key,children='children',retuKey)=>{
    let checkedKey =[];
    data.map(item=>{
        if(item[key]){
            checkedKey.push(item[retuKey])
        }
        if(item.children){
           let retuKeys = signKeys(item.children,key,children,retuKey);
           retuKeys.map(value => {checkedKey.push(value)});
        }
    })
    return checkedKey
};

let viewerEntitiesDict= {};

export const addLayer = (layerAttribute)=>{
  let promise = undefined;
  switch (layerAttribute.type)
  {
    case 'modal':
      console.info("getView() ",getViewer());
      console.info("getViewer().scene " , getViewer().scene);
      console.info("getViewer().scene.open " , getViewer().scene.open);
      promise = getViewer().scene.open(layerAttribute.map_url);
      Cesium.when(promise,function(layers){
        for(let i=0;i<layers.length;i++)
        {
          layers[i].visible=layerAttribute.show;
        }
      });
      break;
    case 'terrain':
      promise = getViewer().scene.open(layerAttribute.map_url);
      Cesium.when(promise,function(layers){
        for(let i=0;i<layers.length;i++)
        {
          layers[i].visible=layerAttribute.show;
        }
      });
      break;
    case 'image':
      promise = getViewer().imageryLayers.addImageryProvider(new Cesium.SuperMapImageryProvider({
          url: layerAttribute.map_url,
          name: layerAttribute.key,
        })
      );
      promise.show = layerAttribute.show;
      break;
    case 'onlineImg':
      let onlineMap = new Cesium.UrlTemplateImageryProvider({
      url: layerAttribute.map_url
    });
      promise = getViewer().imageryLayers.addImageryProvider(onlineMap,1);
      promise.show = layerAttribute.show;
      break;
  }
};

export const setLayerVisible = (layerAttribute,visible) => {
  switch (layerAttribute.type)
  {
    case 'modal':
      fetch(layerAttribute.map_url+'/scenes.json')
        .then(function(response) {
          return response.json();
        }).then(function(data){
        data.map(sceneName =>{
          fetch(layerAttribute.map_url+'/scenes/'+sceneName.name+'/layers.json')
            .then(function(response) {
              return response.json();
            }).then(function(layerData){
            layerData.map(layerName =>{
              let layer = getViewer().scene.layers.find(layerName.name);
              layer.visible = visible;
            });
          }.bind(this));
        });
      }.bind(this));
      break;
    case 'terrain':
      if(visible){getViewer().scene.open(layerAttribute.map_url);}
      else {getViewer().scene.terrainProvider = new Cesium.EllipsoidTerrainProvider({});}
      break;
    case 'image':
      let imageLayers = getViewer().scene.imageryLayers._layers;
      imageLayers.map(layer=>{
        if(layer.imageryProvider.name===layerAttribute.key)
        {
          layer.show = visible;
        }
      });
      break;
    case 'onlineImg':
      let imageLayers2 = getViewer().scene.imageryLayers._layers;
      imageLayers2.map(layer=>{
        if(layer.imageryProvider.name===layerAttribute.layerName)
        {
          layer.show = visible;
        }
      });
      break
  }
};

export {
  viewerEntitiesDict
}
